<?php

class InfoAction extends CommonAction{

    public function __construct(){

        parent::__construct();

    }

    /**
     * 站内信列表
     * @return [type] [description]
     */
    public function index(){

        $list = M('member_info')
                ->where(array(
                    'to_uid' => uid()
                ))
                ->join('tc_member ON tc_member_info.from_uid = tc_member.id')
                ->field('tc_member.account,tc_member_info.*')
                ->order('create_time desc')
                ->select();

        $this->list = $list;

        // echo M('member_info')->_sql();
        // p($list);

    	$this->display();
    }

    /**
     * 站内信详情
     * @return [type] [description]
     */
    public function detail(){
        $map['tc_member_info.id'] = I('get.id');

        $info = M('member_info')
                ->where($map)
                ->join('tc_member ON tc_member_info.from_uid = tc_member.id')
                ->field('tc_member.account,tc_member_info.*')
                ->find();
        p($info);


        // $this->info = $info;

        M('member_info')->where($map)->setField('status',1);


    	// $this->display();
    }

    /**
     * 发送站内信
     * 1、在对应好友发送按钮后面发起。
     * 2、选择好友列表发起
     * @return [type] [description]
     */
    public function send(){
    	// 测试-获取所有会员
    	$map['id'] = array('NEQ',uid());
    	$members = M('member')->field('id,account')->where($map)->select();
    	// echo M('member')->getLastSql();
    	$this->members = $members;

    	$this->display();
    }

    /**
     * 处理发送
     * 如果记录到memcache
     * @return [type] [description]
     */
    public function handleSend(){
    	// p($_POST);

    	$data = array(
    		// 'id'	   => 2,
    		'from_uid' => uid(),
    		'to_uid'   => I('post.to_uid'),
    		'content'  => I('post.content'),
    		'create_time' => time(),
    		// 'status'   => 0,
    	);
    	// p($data);
    	$res = M('member_info')->add($data);
    	// $res = M('member_info')->query(" INSERT INTO `tc_member_info` (`from_uid`,`to_uid`,`content`,`create_time`) VALUES (".uid().",".I('post.to_uid').",'".I('post.content')."',".time().")");
    	// var_dump($res);
    	// echo M('member_info')->_sql();
    	// echo M('member_info')->getError();
    	// exit;
    	$res?$this->success('发送成功'):$this->error('发送失败：'.M('member_info')->getError());
    }

    /**
     * 获取未读站内信数量
     * @return [type] [description]
     */
    public function unreadInfoNum(){
    	if(AJAX){
	        /* 未读站内信 */
	        $info_num = M('member_info')->where(array(
	        	'to_uid' => uid(),
	        	'status' => 0,
	        ))->count();
            // echo M('member_info')->_sql();
	        echo $info_num;
    	}
    }


}